#!/usr/bin/env python
# coding: utf-8

# # 自动翻译器
# 
# ## 一、设计思路
# 
# 1.qt提取剪贴板内容作为翻译内容
# 
# 2.使用python向百度翻译提交翻译内容，然后取回翻译结果
# 
# 3.使用qt显示翻译结果
# 

# In[71]:


import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


# In[122]:


class Translation:
    def __init__(self):
        self.options = webdriver.ChromeOptions()   
        self.options.add_argument('headless')    # 后台运行
        self.options.add_argument('log-level=3')
#         self.options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
        # 禁止图片的加载
        self.prefs = {"profile.managed_default_content_settings.images":2}
        self.options.add_experimental_option("prefs",self.prefs)
        self.browser = webdriver.Chrome(executable_path='chromedriver.exe', options=self.options)#, desired_capabilities=self.desired_capabilities)

        self.load = False
        self.txt = ''
        
    def __del__(self):
        self.browser.close()
        
    def translate(self, words):
        try:
            if not self.load :
                self.load = True
                self.browser.get('https://fanyi.baidu.com/translate#en/zh/')
            self.input= self.browser.find_element_by_id('baidu_translate_input')
            self.input.clear()
            self.input.send_keys(words)
            
            self.button = self.browser.find_element_by_id('translate-button')
            self.button.click()

            time.sleep(1)

            self.out = self.browser.find_element_by_class_name('output-bd')
            self.txt = self.out.text

        finally:
            pass
#             print ("translate [ {0} ] error.".format(words))
        return (self.txt)
        


# In[126]:


if __name__ == '__main__':
    words = [
        '0',
        'Taylor was nominated for a Primetime Emmy Award last year for portraying Minnie in the latest Mickey Mouse TV show rendition ',
        '"I really want whoever comes after us to be aware of the history and the tradition, and to love the characters as much as we do," Taylor said about herself and Allwine, according to Disney.',
        '"Minnie Mouse lost her voice with the passing of Russi Taylor," Bob Iger, Disney Chairman and CEO, wrote on Twitter.'
    ]

    print ('---start-')
    translation = Translation()
    for w in words:
        print ('----', w)
        result = translation.translate(w)
        print (result)
    
    del translation
    
    


# In[ ]:





# In[ ]:




